package com.yiyuwanglu.basecore.web.controller;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.yiyuwanglu.basecore.common.Pager;
import com.yiyuwanglu.basecore.service.impl.ShopDataServiceImpl;
import com.yiyuwanglu.basecore.util.TimesUtil;
import com.yiyuwanglu.basecore.vo.WapDataVo;
import com.yiyuwanglu.basecore.web.common.ResultMap;
import com.yiyuwanglu.basecore.web.common.ResultMap.STATUS;
/**
 * 金策师 1024优化统计
 * @author mj
 *
 */
@Controller("shopDataController")
@RequestMapping("/shopData")
public class ShopDataController {

	@Autowired
	private ShopDataServiceImpl shopDataService;

	/**
	 * 
	 * 首页管理 统计
	 */
	@RequestMapping("/shopFrontMeter")
	@ResponseBody
	public ResultMap shopFrontMeter(WapDataVo wap, Pager page) {
		SimpleDateFormat fmt = new SimpleDateFormat("yyyy-MM");
		ResultMap resultMap = new ResultMap();
		if(StringUtils.isBlank(wap.getMonthStart())){
			wap.setMonthStart(fmt.format(new Date()));
		}
		wap.setMonthStart("^"+wap.getMonthStart()+".*");
		
		//访客量
		List<WapDataVo> listView= shopDataService.shopHomeView(wap,"110409");
		//首页管理标签点击次数
		List<WapDataVo> label1= shopDataService.shopHomeLabels(wap,"^11040\\d");
		String date = "";
		int count =0;
		Map<Object,Object> map1 = new HashMap<Object,Object>();
		ArrayList<Object> list = new ArrayList<Object>(); 
		for (int i = 0; i < label1.size(); i++) {
			if(i==0){
				date=label1.get(0).getCreateDate();
			}
			if(!date.equals(label1.get(i).getCreateDate())){
				map1.put("date",date);
				list.add(map1);
				date=label1.get(i).getCreateDate();
				map1 = new HashMap<Object,Object>();
			}
			for (int j = 0; j < listView.size(); j++) {
				if(listView.get(j).getCreateDate().equals(date)){
					count++;
				}
			}
			map1.put("view", count);
			count=0;
			map1.put("A"+label1.get(i).getType(), label1.get(i).getCount());
		}
		map1.put("date",date);
		list.add(map1);
		
		
		resultMap.setStatus(STATUS.SUCCESS);
		resultMap.setAttribute("pager", page);
		resultMap.setAttribute("label1",list);
		
		return resultMap;
	}
	
	
	
	/**
	 * 
	 * 产品管理页 统计
	 */
	@RequestMapping("/shopProductMeter")
	@ResponseBody
	public ResultMap shopProductMeter(WapDataVo wap, Pager page) {
		SimpleDateFormat fmt = new SimpleDateFormat("yyyy-MM");
		ResultMap resultMap = new ResultMap();
		if(StringUtils.isBlank(wap.getMonthStart())){
			wap.setMonthStart(fmt.format(new Date()));
		}
		wap.setMonthStart("^"+wap.getMonthStart()+".*");
		
		//访客量
		List<WapDataVo> listView= shopDataService.shopHomeView(wap,"110500");
		//首页管理标签点击次数
		List<WapDataVo> label1= shopDataService.shopHomeLabels(wap,"^11050.*");
		String date = "";
		int count =0;
		Map<Object,Object> map1 = new HashMap<Object,Object>();
		ArrayList<Object> list = new ArrayList<Object>(); 
		for (int i = 0; i < label1.size(); i++) {
			if(i==0){
				date=label1.get(0).getCreateDate();
			}
			if(!date.equals(label1.get(i).getCreateDate())){
				map1.put("date",date);
				list.add(map1);
				date=label1.get(i).getCreateDate();
				map1 = new HashMap<Object,Object>();
			}
			for (int j = 0; j < listView.size(); j++) {
				if(listView.get(j).getCreateDate().equals(date)){
					count++;
				}
			}
			map1.put("view", count);
			count=0;
			map1.put("A"+label1.get(i).getType(), label1.get(i).getCount());
		}
		
		map1.put("date",date);
		list.add(map1);
		
		resultMap.setStatus(STATUS.SUCCESS);
		resultMap.setAttribute("pager", page);
		resultMap.setAttribute("label1",list);
		
		return resultMap;
	}
	
	
	/**
	 * 
	 * 管理页 统计
	 * 根据类型
	 * 
	 */
	@RequestMapping("/shopMeter")
	@ResponseBody
	public ResultMap shopMeter(WapDataVo wap, Pager page,HttpServletRequest request) {
		SimpleDateFormat fmt = new SimpleDateFormat("yyyy-MM");
		ResultMap resultMap = new ResultMap();
		if(StringUtils.isBlank(wap.getMonthStart())){
			wap.setMonthStart(fmt.format(new Date()));
		}
		wap.setMonthStart("^"+wap.getMonthStart()+".*");
		
		String type = request.getParameter("type");
		String types = request.getParameter("types");
		
		//访客量
		List<WapDataVo> listView= shopDataService.shopHomeView(wap,type);
		//首页管理标签点击次数
		List<WapDataVo> label1= shopDataService.shopHomeLabels(wap,"^"+types+".*");
		String date = "";
		int count =0;
		Map<Object,Object> map1 = new HashMap<Object,Object>();
		ArrayList<Object> list = new ArrayList<Object>(); 
		for (int i = 0; i < label1.size(); i++) {
			if(i==0){
				date=label1.get(0).getCreateDate();
			}
			if(!date.equals(label1.get(i).getCreateDate())){
				map1.put("date",date);
				list.add(map1);
				date=label1.get(i).getCreateDate();
				map1 = new HashMap<Object,Object>();
			}
			for (int j = 0; j < listView.size(); j++) {
				if(listView.get(j).getCreateDate().equals(date)){
					count++;
				}
			}
			map1.put("view", count);
			count=0;
			map1.put("A"+label1.get(i).getType(), label1.get(i).getCount());
		}
		
		map1.put("date",date);
		list.add(map1);
		
		resultMap.setStatus(STATUS.SUCCESS);
		resultMap.setAttribute("pager", page);
		resultMap.setAttribute("label1",list);
		
		return resultMap;
	}
	
	
	//内部理财师统计
	@RequestMapping("/shopFaUserMeter")
	@ResponseBody
	public ResultMap shopFaUserMeter(WapDataVo wap, Pager page,HttpServletRequest request) throws ParseException {
		
		SimpleDateFormat day = new SimpleDateFormat("yyyy-MM-dd");
		ResultMap resultMap = new ResultMap();
	
		//模糊查询  
		if(StringUtils.isBlank(wap.getMonthStart())){
			wap.setMonthStart(day.format(new Date()));
		}
		String st = wap.getMonthStart();
		wap.setMonthStart("^"+wap.getMonthStart()+".*");
		
		
		String mobiles=request.getParameter("mobiles");
		if(StringUtils.isBlank(mobiles)){
			mobiles=null;
		}
		
		String types = "110401,110402,110403,110404,110405,110406,110503,110408";
		List<WapDataVo> shopFaUser = shopDataService.shopFaUser(wap, types,mobiles,page);
		//查询店铺访问增量传入日期-1day
		wap.setMonthStart(day.format(TimesUtil.timsUpd(day.parse(st),-1))+" 23:59:59");
		List<WapDataVo> shopViewListDay = shopDataService.shopViewList(wap,mobiles,"^11010\\d",page);
		//查询店铺访问量总
		wap.setMonthStart(st+" 23:59:59");
		List<WapDataVo> shopViewListAll = shopDataService.shopViewList(wap,mobiles,"^11010\\d",page);
		
		wap.setMonthStart(st+" 23:59:59");
		List<WapDataVo> shopIdAll = shopDataService.shopIdList(wap,mobiles,"110409",page);
		int  count =shopDataService.shopIdListAll(wap,mobiles,"110409");;
		
		//集合存放
		ArrayList<Object> list = new ArrayList<Object>();
		//shopIdAll
		for (int i = 0; i < shopIdAll.size(); i++) {
			if(StringUtils.isNotBlank(shopIdAll.get(i).getShopId())){
				TreeMap<Object,Object> map = new TreeMap<Object,Object>();
				map.put("shopId",shopIdAll.get(i).getShopId());
				for (int j = 0; j < shopFaUser.size(); j++) {
					if(shopIdAll.get(i).getShopId().equals(shopFaUser.get(j).getShopId())){
						map.put("A"+shopFaUser.get(j).getType(),shopFaUser.get(j).getCount());
					}
				}
				for (int k = 0; k < shopViewListDay.size(); k++) {
					if(shopViewListDay.get(k).getShopId().equals(shopIdAll.get(i).getShopId())){
						map.put("seeShopDay",Integer.valueOf(Integer.valueOf(shopViewListDay.get(k).getCount())) );
						map.put("viewShopDay",Integer.valueOf(Integer.valueOf(shopViewListDay.get(k).getViewCount())) );
					}
				}
				for (int k = 0; k < shopViewListAll.size(); k++) {
					if(shopViewListAll.get(k).getShopId().equals(shopIdAll.get(i).getShopId())){
						map.put("seeShopAll",shopViewListAll.get(k).getCount() );
						map.put("viewShopAll",shopViewListAll.get(k).getViewCount() );
					}
				}
				if(map.get("seeShopAll")!=null && map.get("seeShopDay")!=null){
					
					map.put("seeShopDay",Integer.valueOf(map.get("seeShopAll").toString())-
							Integer.valueOf(map.get("seeShopDay").toString()));
				}
				
				if(map.get("viewShopAll")!=null && map.get("viewShopDay")!=null){
					map.put("viewShopDay",Integer.valueOf(map.get("viewShopAll").toString())-
							Integer.valueOf(map.get("viewShopDay").toString()));
				}
				
				map.put("faName","");
				map.put("date",st);
				list.add(map);
			}
		}
		page.setTotalCount(count);
		resultMap.setStatus(STATUS.SUCCESS);
		resultMap.setAttribute("pager", page);
		resultMap.setAttribute("list", list);
		return resultMap;
	}
	
	
	/**
	 * 根据 多手机号
	 * type 统计总数
	 */
	@RequestMapping("/getByMobileType")
	@ResponseBody
	public ResultMap getByMobileType(WapDataVo wap, Pager page,HttpServletRequest request) {
		ResultMap resultMap = new ResultMap();
		String mobiles=request.getParameter("mobiles");
		String types=request.getParameter("types");
		if(StringUtils.isBlank(mobiles)){
			mobiles="18322224444";
		}
		List<WapDataVo> list = shopDataService.getByMobileType(wap,types,mobiles,page);
		
		resultMap.setStatus(STATUS.SUCCESS);
		resultMap.setAttribute("pager", page);
		resultMap.setAttribute("list", list);
		return resultMap;
	}
	
	
	
	//总数统计
	@RequestMapping("/getAll")
	@ResponseBody
	public ResultMap getAll(WapDataVo wap, Pager page,HttpServletRequest request) throws ParseException {
		ResultMap resultMap = new ResultMap();
		SimpleDateFormat day = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		
		if(StringUtils.isBlank(wap.getMonthEnd())){
			wap.setMonthEnd(day.format(new Date()));
		}
		else{
			wap.setMonthEnd(wap.getMonthEnd()+" 23:59:59");
		}
		
		//按钮次数等统计
		String types ="110900,110503,110504,110506,110408,110100,110201,110202,110203,110204,110209,110505";
		List<WapDataVo> all = shopDataService.getAll(wap,types,page);
		//int allCount = shopDataService.getAllCount(wap,types,page);
		
		
		String date = "";
		Map<Object,Object> map1 = new HashMap<Object,Object>();
		ArrayList<Object> list = new ArrayList<Object>(); 
		for (int i = 0; i < all.size(); i++) {
			
			int a = 0;
			if(i==0){
				date=all.get(0).getCreateDate();
			}
			if(!date.equals(all.get(i).getCreateDate())){
				map1.put("date",date);
				list.add(map1);
				date=all.get(i).getCreateDate();
				map1 = new HashMap<Object,Object>();
			}
			if(all.get(i).getType().contains("11020")){
				a+= Integer.valueOf(all.get(i).getCount());
				map1.put("A110200", a+"");
			}else{
				map1.put("A"+all.get(i).getType(), all.get(i).getCount());
			}
			
		}
		
		map1.put("date",date);
		list.add(map1);
		
		page.setTotalCount(list.size());
		//外部分页
		Integer pageEnd = list.size(); 
		if(pageEnd>(page.getPageSize()*page.getPageNo())){
			pageEnd=page.getPageSize()*page.getPageNo();
		}
		List<Object> l = new ArrayList<Object>();
		for (int i = page.getPageSize()*(page.getPageNo()-1); i < pageEnd; i++) {
			l.add(list.get(i));
		}
		page.setTotalCount(list.size());
		resultMap.setStatus(STATUS.SUCCESS);
		resultMap.setAttribute("pager", page);
		resultMap.setAttribute("list", l);
		
		
		return resultMap;
	}
}
